home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 4.6 KB | 158 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWPriMem.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWFound.hpp"
-
- #ifndef FWPRIMEM_H
- #include "FWPriMem.h"
- #endif
-
- #ifndef SLPRIDEB_H
- #include "SLPriDeb.h"
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWCommon
- #endif
-
- typedef void (*pfvv)();
- extern pfvv set_new_handler(pfvv);
-
- //========================================================================================
- // set_new_handler
- //========================================================================================
-
- // We (optionally) provide our own definition of set_new_handler mainly as a precaution against
- // ending up with a shared library version of set_new_handler. ODF as a whole has been
- // written assuming that operator new will throw an exception if it fails to allocate
- // memory. In a runtime environment such as OpenDoc, it's possible for multiple shared
- // libraries to exist in one process. An exception thrown from code in one shared
- // library should not propogate outside of the shared library (since there is no agreed
- // binary interface for exceptions across shared libraries). This implies that if
- // operator new throws exceptions then operator new cannot be implemented in a shared
- // library. In actuality, it's the "new hander" that throws, so there must be one new
- // handler per shared library, and thus one version of set_new_handler per shared library.
- // This unit (FWPriMem.cpp) is intended to be statically linked into each shared library
- // built with ODF.
-
- #if defined(FW_DONT_USE_STD_LIB_SET_NEW_HANDLER)
-
- static pfvv gNewHandler = 0;
-
- #ifndef _MSC_VER
- pfvv set_new_handler(pfvv handler)
- {
- // See ARM, pp 280-81.
- pfvv oldHandler = gNewHandler;
- gNewHandler = handler;
- return oldHandler;
- }
- #endif
-
- #ifdef _MSC_VER
-
- #include <new.h>
-
- pfvv __cdecl set_new_handler(pfvv handler)
- {
- // See ARM, pp 280-81.
- pfvv oldHandler = gNewHandler;
- gNewHandler = handler;
- return oldHandler;
- }
-
- // [KVV] Because of the way Visual C++ 4.0 linker/library work,
- // we have to define the following stuff
-
- _PNH _set_new_handler(_PNH /* pnh */)
- {
- return 0;
- }
-
- _PNH _query_new_handler()
- {
- return 0;
- }
-
- extern "C" int _callnewh(size_t /* size */)
- {
- return 1;
- }
-
- #endif
-
- #endif
-
- //========================================================================================
- // C++ Global operator new & delete
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // operator new, placed memory version
- //----------------------------------------------------------------------------------------
-
- // already defines placement new in <new.h>
- // Turn off definition if we ought not define it
- #define FW_NEW_WITH_PLACEMENT
-
- #if defined(_MSC_VER)
- #undef FW_NEW_WITH_PLACEMENT
- #elif defined(SYMANTEC_CPLUS)
- #undef FW_NEW_WITH_PLACEMENT
- #elif defined(__MWERKS__)
- #if __option(dont_inline)
- #undef FW_NEW_WITH_PLACEMENT
- #endif
- #endif
-
- #ifdef FW_NEW_WITH_PLACEMENT
- void *operator new(size_t /* size */,void *p)
- {
- return p;
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // operator new
- //----------------------------------------------------------------------------------------
- void *operator new(size_t size)
- {
- void* p;
-
- while ((p = FW_PrimitiveAllocateBlock(size)) == NULL)
- {
- pfvv newHandler;
- // this is just a cheap way to get the newHandler function pointer
- set_new_handler(newHandler = set_new_handler(0));
- FW_PRIV_ASSERT(newHandler != NULL);
- // ODF is written assuming that operator new will not return if it fails to
- // allocate memory. It is assumed that a newHandler will be installed which
- // throws an exception. ODF installs a newHandler in a higher layer (FWODExce)
- // so a newHandler will exist unless a developer chooses to intentionally
- // deinstall the newHandler by calling ::set_new_handler(0). Doing so will
- // break ODF's error handling strategy and is considered illegal. Note
- // that developers are free to install their own newHandler.
- if (!newHandler)
- return 0;
- else
- newHandler();
- }
-
- return p;
- }
-
- //----------------------------------------------------------------------------------------
- // operator delete
- //----------------------------------------------------------------------------------------
- void operator delete(void *p)
- {
- FW_PrimitiveFreeBlock(p);
- }
-
-